Assignment #128 Summing Several Numbers from Any File
Code
///Name: Ian Grant
///Period:5
///Project Name: Summing Several Numbers from Any File
///File Name:SumFile.java
///06/08/16
import java.io.File;
import java.util.Scanner;
public class SumFile {
public static void main(String[] args) throws Exception
{
Scanner stuff = new Scanner(System.in);
System.out.println("Which file you want: ");
String imput = stuff.next();
Scanner fileIn = new Scanner(new File((String) imput));
int total = 0;
while (fileIn.hasNext())
{
int print = fileIn.nextInt();
System.out.print(print + " ");
total = total + print;
}
fileIn.close();
System.out.println("The total is " + total);
fileIn.close();
}
}
Output